// packageH/stock/guide/guide.js
const App = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showExplainPopup: false,
    goodsList: [],
    searchGoodsList: null,
    sets: null,
    loading: false,
    isMoreData: true,
    page: 1,
    searchInputKeyword: "",
    is_retail:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.target == 'retail'){
      wx.setNavigationBarTitle({
        title: '零售',
      });
      this.setData({
        is_retail:true
      });
    }else{
      wx.setNavigationBarTitle({
        title: '订货',
      });
      this.setData({
        is_retail:false
      });
    }
    this.getGoods();
    
  },
  onShareAppMessage() {},
  onShareTimeline() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getGoods();
  },
  displayExplainPopup() {
    this.setData({
      showExplainPopup: true,
    });
  },
  closeExplainPopup() {
    this.setData({
      showExplainPopup: false,
    });
  },
  viewGoods(event) {
    let _url = "/packageA/detail_v2/detail_v2?id=" + event.target.dataset.id;
    if(this.data.is_retail){
      _url = _url +'&fromStock=2';
    }else{
      _url = _url +'&fromStock=1';
    }
    wx.navigateTo({ url: _url });
  },
  getGoods() {
    if (this.data.loading === true || this.data.isMoreData === false) {
      return;
    }
    wx.showLoading({
      title: "加载中",
      mask: true,
    });
    this.setData({
      loading: true,
    });
    let urlStr = App.getNetAddresss("plugin.agency.api.goods.goods-list");
    urlStr += "&page=" + this.data.page;
    urlStr += "&is_retail=" + this.data.is_retail;
    App._postNetWork({
      url: urlStr,
      success: ({ data: { data, result, msg } }) => {
        if (result === 0) {
          wx.hideLoading();
          wx.showToast({
            title: msg,
            success: (result) => {
              wx.redirectTo({
                url: "/packageG/member_v2/member_v2",
              });
            },
          });
          return;
        }
        const setData = {};
        if (this.data.sets === null) {
          setData["sets"] = data.set;
        }
        if (
          data.current_page === data.last_page ||
          data.data.length === 0 ||
          data.data.length < data.per_page
        ) {
          setData["isMoreData"] = false;
        }
        let goodsList = this.data.goodsList;
        goodsList.push(...data.data);
        setData["goodsList"] = goodsList;
        setData["page"] = this.data.page + 1;
        setData["loading"] = false;

        this.setData(setData);

        wx.hideLoading();
      },
    });
  },
  searchGoods(event) {
    const keyword = event.detail;
    let searchResult = this.data.goodsList.filter((item) => {
      return item.title.indexOf(keyword) !== -1;
    });
    this.setData({
      searchGoodsList: searchResult,
    });
  },
  cancelSearch(){
    this.setData({
      searchGoodsList: null,
    });
  }
});
